package Messages;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;

/**
 * This class implements the GetRentBuss from the DB
 *@author Gabi Revah
 *@author Aviad Almog
 *
 */
public class MessageGetRentBuss  {
	
	private ConnectionToClient client ;
	private String[] str;

	/**
	 *  Constructor of this class 
	 * @param str
	 * @param client
	 */
	public MessageGetRentBuss (String[] str, ConnectionToClient client) {
		
		this.client= client ;
		this.str= str; 	
	
	}
	
/**
 * This method gets all the business customers that have at least one rent order from the DB	
 * @throws SQLException
 * @throws IOException
 */
public void GetRentBuss() throws SQLException, IOException{
		
		SQLConnection sc = new SQLConnection();
		java.sql.PreparedStatement ps = sc.getConnection().prepareStatement("SELECT DISTINCT rentalorders.bussID FROM db.rentalorders");
		ResultSet bussIDRes= ps.executeQuery();
		ArrayList<Object> bussDetailes=new ArrayList<Object>();
		bussDetailes.add("GetRentBuss");
		while (bussIDRes.next())
		{
			java.sql.PreparedStatement op = sc.getConnection().prepareStatement("SELECT business.Name FROM db.business where BusinessId='"+bussIDRes.getString(1)+"'");
			ResultSet bussNameRes= op.executeQuery();
			bussDetailes.add(""+bussIDRes.getString(1));
			while (bussNameRes.next())
			{
				bussDetailes.add(""+bussNameRes.getString(1));
			}
		}
		client.sendToClient(bussDetailes);
		}

}
